﻿using DotNetSpeech;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Calculator
{
    public partial class CalculateBoard : Form
    {
        public CalculateBoard()
        {
            InitializeComponent();
        }
        double a = 0; //第一个数
        double b = 0;//   二
        double Ans = 0;//
        double c = 0;//计算结果
        SpVoice voice = new DotNetSpeech.SpVoiceClass();


        Charactor charactor = Charactor.None;
        
        bool inputflag = false; //false代表正在输入第一个数字，true代表正在输入第二个数字
        enum Charactor
        {
            Power,
            Plus,
            Minus,
            Multiply,
            Devide,
            Percent,
            Sin,
            Cos,
            Tan,
            Cot,
            Sec,
            Csc,
            Factorial,
            TenPower,
            Squaret,
            Log,
            Ln,
            Arcsin,
            Arccos,
            Arctan,
            Arccot,
            Arcsec,
            Arccsc,
            Rooting,

            None
        }
        




        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button17_Click(object sender, EventArgs e)
        {
            voice.Speak("九", SpeechVoiceSpeakFlags.SVSFlagsAsync);

            switch (inputflag)
            {
                case false://正在输入第一个数字
                    {
                        a = a * 10 + 9;
                        ShowOnDisplay(a.ToString());
                        break;
                    }
                case true://正在输入第二个数字
                    {
                        b = b * 10 + 9;
                        ShowOnDisplay(b.ToString());
                        break;
                    }
            }
        }

        private void ShowOnDisplay(double a)
        {
            //throw new NotImplementedException();
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            voice.Speak("乘", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("×");
            inputflag = true;
            charactor = Charactor.Multiply;
        }

        private void btn_num1_Click(object sender, EventArgs e)
        {
            voice.Speak("一", SpeechVoiceSpeakFlags.SVSFlagsAsync);

            switch (inputflag)
            {
                case false://正在输入第一个数字
                    {
                        a = a * 10 + 1;
                        ShowOnDisplay(a.ToString());
                        break;
                    }
                case true://正在输入第二个数字
                    {
                        b = b * 10 + 1;
                        ShowOnDisplay(b.ToString());
                        break;
                    }
            }
        }

        private void CalculateBoard_Load(object sender, EventArgs e)
        {

        }
        private void ShowOnDisplay(float num)
        {
            Graphics graphics = Display.CreateGraphics();
            graphics.Clear(Color.White);
            Font myfont = new Font("宋体",25);
            graphics.DrawString(num.ToString(), myfont, Brushes.Black, 20, 20);
            
        }
        private void ShowOnDisplay(string content)
        {
            Graphics graphics = Display.CreateGraphics();
            graphics.Clear(Color.White);
            Font myfont = new Font("宋体", 25);
            graphics.DrawString(content, myfont, Brushes.Black, 20, 20);

        }

        private void btn_num2_Click(object sender, EventArgs e)
        {
            voice.Speak("二", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            switch (inputflag)
            {
                case false://正在输入第一个数字
                    {
                        a = a * 10 + 2;
                        ShowOnDisplay(a.ToString());
                        break;
                    }
                case true://正在输入第二个数字
                    {
                        b = b * 10 + 2;
                        ShowOnDisplay(b.ToString());
                        break;
                    }
            }
        }

        private void btn_num3_Click(object sender, EventArgs e)
        {
            voice.Speak("三", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            switch (inputflag)
            {
                case false://正在输入第一个数字
                    {
                        a = a * 10 + 3;
                        ShowOnDisplay(a.ToString());
                        break;
                    }
                case true://正在输入第二个数字
                    {
                        b = b * 10 + 3;
                        ShowOnDisplay(b.ToString());
                        break;
                    }
            }
        }

        private void btn_num4_Click(object sender, EventArgs e)
        {
            voice.Speak("四", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            switch (inputflag)
            {
                case false://正在输入第一个数字
                    {
                        a = a * 10 + 4;
                        ShowOnDisplay(a.ToString());
                        break;
                    }
                case true://正在输入第二个数字
                    {
                        b = b * 10 + 4;
                        ShowOnDisplay(b.ToString());
                        break;
                    }
            }
        }

        private void btn_num5_Click(object sender, EventArgs e)
        {
            voice.Speak("五", SpeechVoiceSpeakFlags.SVSFlagsAsync);

            switch (inputflag)
            {
                case false://正在输入第一个数字
                    {
                        a = a * 10 + 5;
                        ShowOnDisplay(a.ToString());
                        break;
                    }
                case true://正在输入第二个数字
                    {
                        b = b * 10 + 5;
                        ShowOnDisplay(b.ToString());
                        break;
                    }
            }
        }

        private void btn_num6_Click(object sender, EventArgs e)
        {
            voice.Speak("六", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            
            switch (inputflag)
            {
                case false://正在输入第一个数字
                    {
                        a = a * 10 + 6;
                        ShowOnDisplay(a.ToString());
                        break;
                    }
                case true://正在输入第二个数字
                    {
                        b = b * 10 + 6;
                        ShowOnDisplay(b.ToString());
                        break;
                    }
            }
        }

        private void btn_num7_Click(object sender, EventArgs e)
        {
            voice.Speak("七", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            
            switch (inputflag)
            {
                case false://正在输入第一个数字
                    {
                        a = a * 10 + 7;
                        ShowOnDisplay(a.ToString());
                        break;
                    }
                case true://正在输入第二个数字
                    {
                        b = b * 10 + 7;
                        ShowOnDisplay(b.ToString());
                        break;
                    }
            }
        }

        private void btn_num8_Click(object sender, EventArgs e)
        {
            voice.Speak("八", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            
            switch (inputflag)
            {
                case false://正在输入第一个数字
                    {
                        a = a * 10 + 8;
                        ShowOnDisplay(a.ToString());
                        break;
                    }
                case true://正在输入第二个数字
                    {
                        b = b * 10 + 8;
                        ShowOnDisplay(b.ToString());
                        break;
                    }
            }
        }

        private void btn_num0_Click(object sender, EventArgs e)
        {
            voice.Speak("零", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            
            switch (inputflag)
            {
                case false://正在输入第一个数字
                    {
                        a = a * 10;
                        ShowOnDisplay(a.ToString());
                        break;
                    }
                case true://正在输入第二个数字
                    {
                        b = b * 10;
                        ShowOnDisplay(b.ToString());
                        break;
                    }
            }
        }

        private void btn_plus_Click(object sender, EventArgs e)
        {
            voice.Speak("加", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("+");
            inputflag = true;
            charactor = Charactor.Plus;

        }

        private void btn_minus_Click(object sender, EventArgs e)
        {
            voice.Speak("减", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("-");
            inputflag = true;
            charactor = Charactor.Minus;
        }

        private void btn_devide_Click(object sender, EventArgs e)
        {
            voice.Speak("除以", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("÷");
            inputflag = true;
            charactor = Charactor.Devide;
        }

        private void btn_equal_Click(object sender, EventArgs e)
        {
            voice.Speak("等于", SpeechVoiceSpeakFlags.SVSFlagsAsync);
                        
            if (inputflag == false)
            {
                switch (charactor)
                {
                    case Charactor.Rooting:
                        {

                            break;
                        }
                    case Charactor.Ln:
                        {
                            c = Math.Log(a);
                            ShowOnDisplay("=" + c.ToString());
                            if (c >= 0)
                            {
                                voice.Speak(c.ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" + (-c).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            Ans = c;
                            a = c;
                            break;
                            
                        }
                    case Charactor.Squaret:
                        {
                            if (a >= 0)
                            {
                                a = Math.Sqrt(a);
                                ShowOnDisplay("=" + a.ToString());
                                voice.Speak(a.ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                                Ans = a;
                            }
                            else
                            {
                                ShowOnDisplay("负数没有平方根！");
                            }

                            break;
                        }
                    case Charactor.TenPower:
                        {
                            a = Math.Pow(10, a);

                            if (a >= 0)
                            {
                                voice.Speak(a.ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" + (-a).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            Ans = a;
                            ShowOnDisplay("=" + a.ToString());
                            break;
                            
                        }
                    case Charactor.Factorial:
                        {
                            double i = a-1;
                            double m = a;
                            while (i>0)
                            {
                                m = m * i;
                                i = i - 1;
                            }
                            ShowOnDisplay(a.ToString() + "!=" + m.ToString());
                            a = m;
                            if (a >= 0)
                            {
                                voice.Speak(a.ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" + (-a).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            Ans = a;
                            break;
                        }
                    case Charactor.None:
                        {
                            ShowOnDisplay(a.ToString());
                            if (a >= 0)
                            {
                                voice.Speak(a.ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" +(-a).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            Ans = a;
                            break;
                        }
                    case Charactor.Percent:
                        {
                            
                            ShowOnDisplay("="+(100*a).ToString() + "%");
                            if (a >= 0)
                            {
                                voice.Speak("百分之" + (100*a).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负百分之" + (-100 * a).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }

                            Ans = a;
                            
                            break;
                        }
                    case Charactor.Sin:
                        {
                            a = Math.Sin(a);
                            if (a >= 0)
                            {
                                voice.Speak(a.ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" + (-a).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            Ans = a;
                            ShowOnDisplay("="+a.ToString());
                            break;
                        }
                    case Charactor.Cos:
                        {
                            a = Math.Cos(a);
                            if (a >= 0)
                            {
                                voice.Speak(a.ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" + (-a ).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            Ans = a;
                            ShowOnDisplay("="+a.ToString());
                            break;
                        }
                    case Charactor.Tan:
                        {
                            a = Math.Tan(a);
                            if (a >= 0)
                            {
                                voice.Speak(a.ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" + (-a).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            Ans = a;
                            ShowOnDisplay("="+a.ToString());
                            break;
                        }
                    case Charactor.Cot:
                        {
                            a = 1/Math.Tan(a);
                            if (a >= 0)
                            {
                                voice.Speak(a.ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" + (-a).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            Ans = a;
                            ShowOnDisplay("="+a.ToString());
                            break;
                        }
                    case Charactor.Sec:
                        {
                            a = 1 / Math.Cos(a);
                            if (a >= 0)
                            {
                                voice.Speak(a.ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" + (-a).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            Ans = a;
                            ShowOnDisplay("="+a.ToString());
                            break;
                        }
                    case Charactor.Csc:
                        {
                            a = 1 / Math.Sin(a);
                            if (a >= 0)
                            {
                                voice.Speak(a.ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" + (-a).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            Ans = a;
                            ShowOnDisplay("="+a.ToString());
                            break;
                        }

                }
                inputflag = false;
                

            }
            else
            {
                switch (charactor)
                {
                    case Charactor.Log:
                        {
                            if (a <= 0)
                            {
                                ShowOnDisplay("负数和0不能做底数！");
                                voice.Speak("负数和零不能做底数", SpeechVoiceSpeakFlags.SVSFlagsAsync);

                            }
                            else if (a == 1)
                            {
                                ShowOnDisplay("1做底数无意义！");
                                voice.Speak("一做底数无意义", SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else if (b <= 0)
                            {
                                ShowOnDisplay("真数必须为正数！");
                                voice.Speak("真数必须为正数", SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                c = Math.Log(b, a);
                                ShowOnDisplay("=" + c.ToString());
                                if (c >= 0)
                                {
                                    voice.Speak(c.ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                                }
                                else
                                {
                                    voice.Speak("负" + (-c).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                                }
                                Ans = c;
                                a = c;
                                break;
                                
                                
                            }
                            break;
                        }

                    case Charactor.Plus:
                        {
                            ShowOnDisplay("="+(a + b));
                            if (a + b >= 0)
                            {
                                voice.Speak((a + b).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" + (-a - b).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }

                            Ans = a + b;
                            a = a + b;
                            
                            break;

                        }
                    case Charactor.Minus:
                        {
                            ShowOnDisplay("="+(a - b));
                            if (a - b >= 0)
                            {
                                voice.Speak((a - b).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" + (-a + b).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }

                            Ans = a - b;
                            a = a - b;
                            
                            
                            break;
                        }
                    case Charactor.Multiply:
                        {
                            ShowOnDisplay("="+(a * b));
                            if (a * b >= 0)
                            {
                                voice.Speak((a * b).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" + (-a * b).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }

                            Ans = a * b;
                            a = a * b;
                            
                            break;
                        }
                    case Charactor.Devide:
                        {
                            ShowOnDisplay("="+(a / b));
                            if (a / b >= 0)
                            {
                                voice.Speak((a / b).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" + (-a / b).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            Ans = a / b;
                            a = a / b;
                            
                            break;
                        }
                    case Charactor.Power:
                        {
                            ShowOnDisplay("=" + Math.Pow(a, b));
                            double c = Math.Pow(a, b);
                            if (c >= 0)
                            {
                                voice.Speak(c.ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            else
                            {
                                voice.Speak("负" + (-c).ToString(), SpeechVoiceSpeakFlags.SVSFlagsAsync);
                            }
                            Ans = c;
                            a = c;
                            break;
                        }
                    
                }
            }
            b = 0;
            inputflag = true;
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            voice.Speak("清零", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("--clear--");
            charactor = Charactor.None;
            a = 0;
            b = 0;
            inputflag = false;
        }

        private void btn_backclear_Click(object sender, EventArgs e)
        {
            voice.Speak("消除", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            if (inputflag)
            {

                string aaa = b.ToString();
                string bbb = aaa.Substring(0,aaa.Length-1);
                if (bbb == "")
                {
                    ShowOnDisplay(0);
                    return;
                }
                b = Convert.ToSingle(bbb);
                ShowOnDisplay(b);
            }
            else
            {
                string aaa = a.ToString();
                string bbb = aaa.Substring(0, aaa.Length - 1);
                if (bbb == "")
                {
                    ShowOnDisplay(0);
                    return;
                }
                a = Convert.ToSingle(bbb);
                ShowOnDisplay(a);
            }
        }

        private void CalculateBoard_FormClosing(object sender, FormClosingEventArgs e)
        {
            voice.Speak("程序退出", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            Environment.Exit(Environment.ExitCode);//退出所有窗口
        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void btn_percent_Click(object sender, EventArgs e)
        {
            voice.Speak("百分之", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("%");
            charactor = Charactor.Percent;
            switch (inputflag)
            {
                case false:
                    {
                        ShowOnDisplay(a.ToString() + "%");
                        a = a / 100;
                        break;
                    }
                case true:
                    {

                        ShowOnDisplay(b.ToString() + "%");
                        b = b / 100;
                        break;
                    }
            }
            
            
        }

        private void btn_pi_Click(object sender, EventArgs e)
        {
            voice.Speak("π",SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("π");
            switch (inputflag)
            {
                case false:
                    {
                        a = Math.PI;
                        break;

                    }
                case true:
                    {
                        b = Math.PI;
                        break;
                    }
            }
        }

        private void btn_sin_Click(object sender, EventArgs e)
        {
            voice.Speak("sine", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("sin");
            charactor = Charactor.Sin;
        }

        private void btn_cos_Click(object sender, EventArgs e)
        {
            voice.Speak("cosine", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("cos");
            charactor = Charactor.Cos;
        }

        private void btn_tan_Click(object sender, EventArgs e)
        {
            voice.Speak("tangent", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("tan");
            charactor = Charactor.Tan;
        }

        private void btn_cot_Click(object sender, EventArgs e)
        {
            voice.Speak("cotangent", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("cot");
            charactor = Charactor.Cot;
        }

        private void but_sec_Click(object sender, EventArgs e)
        {
            voice.Speak("secant", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("sec");
            charactor = Charactor.Sec;
        }

        private void btn_csc_Click(object sender, EventArgs e)
        {
            voice.Speak("cosecant", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("csc");
            charactor = Charactor.Csc;
        }

        private void btn_Ans_Click(object sender, EventArgs e)
        {
            voice.Speak("Ans", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("Ans");
            switch (inputflag)
            {
                case false:
                    {
                        a = Ans;
                        break;
                    }
                case true:
                    {
                        b = Ans;
                        break;
                    }
            }
        }

        private void btn_power_Click(object sender, EventArgs e)
        {
            voice.Speak("乘方", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("^");
            charactor = Charactor.Power;
            inputflag = true;
        }

        private void btn_n1_Click(object sender, EventArgs e)
        {
            voice.Speak("阶乘", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            charactor = Charactor.Factorial;
            ShowOnDisplay("!");
            inputflag = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            voice.Speak("十的乘方", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            charactor = Charactor.TenPower;
            ShowOnDisplay("10^");
            inputflag = false;

        }

        private void btn_Sqr_Click(object sender, EventArgs e)
        {
            voice.Speak("根号", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            charactor = Charactor.Squaret;
            ShowOnDisplay("√￣");

            inputflag = false;
        }

        private void btn_log_Click(object sender, EventArgs e)
        {
            voice.Speak("log", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            charactor = Charactor.Log;
            ShowOnDisplay("log");
            inputflag = true;
        }

        private void btn_e_Click(object sender, EventArgs e)
        {
            voice.Speak("e", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            
            ShowOnDisplay("e");
            switch(inputflag)
            {
                case true:
                    {
                        b = Math.E;
                        break;
                    }
                case false:
                    {
                        a = Math.E;
                        break;
                    }

            }
            
        }

        private void btn_ln_Click(object sender, EventArgs e)
        {
            voice.Speak("ln", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay("ln");
            charactor = Charactor.Ln;
            inputflag = false;
        }

        private void btn_rooting_Click(object sender, EventArgs e)
        {
            voice.Speak(a.ToString() + "次根号", SpeechVoiceSpeakFlags.SVSFlagsAsync);
            ShowOnDisplay(a.ToString() + "√￣");
            charactor = Charactor.Rooting;
            inputflag = true;
        }

        private void btn_shift_Click(object sender, EventArgs e)
        {

        }
    }
}
